const UserModel = require('@/models/user.model')
const ArticleModel = require('@/models/article.model')
const DynamicsModel = require('@/models/dynamics.model')
const ShareCircleModel = require('@/models/share_circle.model')
const UserCollectArticleModel = require('@/models/user_collect_article.model')
const UserFocusUserModel = require('@/models/user_focus_user.model')
const UserAgreeArticleModel = require('@/models/user_agree_article.model')
const { getSessionuserId } = require('@/utils/common.util')
const JsonResult = require('@/utils/httpResponse.unit')

class StatisticsController {
  /**
   * find user rank by article counts
   * @param {*} req
   * @param {*} response
   */
  async userRankByArticleCount (req, response) {
    try {
      const sessionId = await getSessionuserId(req)
      const { pageNo = 1, pageSize = 10 } = req.query
      const data = await UserModel.findUserArticleRank(pageNo, pageSize, sessionId)
      JsonResult.success({
        req,
        data,
        response,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询失败' })
    }
  }

  /**
   * vague search article and user
   * @param {*} req
   * @param {*} response
   */
  async vagueSearchUser (req, response) {
    try {
      const sessionId = await getSessionuserId(req)
      const { pageNo = 1, pageSize = 20, value, type } = req.body
      const Model = type === '1' ? ArticleModel : UserModel
      const data = await Model.vagueFind({ pageNo, pageSize, value }, sessionId)
      JsonResult.success({
        req,
        data,
        response,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询失败' })
    }
  }

  /**
   * statistic user dynamics/article/share_circle/collect/focus/agree count
   * @param {*} req
   * @param {*} response
   */
  async statisticUserActionCount (req, response) {
    try {
      const userId = req.query.userId
      if (!userId) {
        return JsonResult.fail({ req, response, message: 'userId is required' })
      }
      const params = { userId }
      const dynamicsTotal = await DynamicsModel.total(params)
      const articleTotal = await ArticleModel.total(params)
      const shareCircleTotal = await ShareCircleModel.total(userId)
      const collectArticleTotal = await UserCollectArticleModel.total(params)
      const userFocusUserTotal = await UserFocusUserModel.total(params)
      const userAgreeArticleTotal = await UserAgreeArticleModel.total(params)
      const userGetAgreeTotal = await UserAgreeArticleModel.userGetAgreeTotal(userId)
      const articleViewCountTotal = await ArticleModel.viewCountTotal(userId)
      JsonResult.success({
        req,
        data: [
          { name: '用户动态', value: dynamicsTotal, key: 'dynamicsTotal' },
          { name: '发布文章', value: articleTotal, key: 'articleTotal' },
          { name: '发布友圈', value: shareCircleTotal, key: 'shareCircleTotal' },
          { name: '收藏文章', value: collectArticleTotal, key: 'collectArticleTotal' },
          { name: '关注用户', value: userFocusUserTotal, key: 'userFocusUserTotal' },
          { name: '文章点赞', value: userAgreeArticleTotal, key: 'userAgreeArticleTotal' },
          { name: '获得点赞', value: userGetAgreeTotal, key: 'userGetAgreeTotal' },
          { name: '文章被阅读', value: articleViewCountTotal, key: 'articleViewCountTotal' }
        ],
        response,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询失败' })
    }
  }
}

module.exports = new StatisticsController()
